
import java.sql.*;
import java.sql.Connection;
import java.sql.SQLException;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author debroizr
 */
public class Consultation {

    public static void consultation(Connection conn) throws SQLException {
        try {
            /* Choix du niveau d'isolation */
            conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
            
            PreparedStatement stmt = conn.prepareStatement("select c.nomc, c.numclient, rp.numplace, rp.prix, rp.datevolp, rp.numvolp, \n"
                    + "vp.destination, vp.duree, vp.origine from resapassager rp\n"
                    + "join reservation r on r.numresa=rp.numresa\n"
                    + "join client c on c.numclient=r.numclient\n"
                    + "join volspassagers vp on rp.numvolp=vp.numvolp\n"
                    + "where c.numclient = ?");

            stmt.setInt(1, LectureClavier.lireEntier("\nSaisissez le numero d'un client :\n"));

            ResultSet rset = stmt.executeQuery();
            int count = 0;
            if(!rset.next()){
                System.out.println("\nAucune reservation pour ce numero de client.");
            }
            while (rset.next()) {
                if (count == 0) {
                    System.out.println("\nListe des reservations ( " + rset.getString(1) + " : "+ rset.getString(2) +" )");
                    count++;
                }
                System.out.println("\tNumVol : " + rset.getString(6)
                        + "\tNumPlace : " + rset.getString(3)
                        + "\t Date de depart : " + rset.getString(5)
                        + "\t Origine : " + rset.getString(9)
                        + "\t Destination : " + rset.getString(7)
                        + "\t Duree : " + Integer.parseInt(rset.getString(8))/60 + "h" + Integer.parseInt(rset.getString(8))%60
                        + "\t Prix : " + rset.getString(4));
            }
            System.out.println();
            stmt.close();
            rset.close();

            String reponse;
            System.out.println("\nVoulez vous faire une nouvelle recherche ? (O/N)\n");
            reponse = LectureClavier.lireChaine();
            if (reponse.equals("O")) {
                consultation(conn);
            } else {
                AirChance.menu();
            }

        } catch (SQLException e) {
            System.err.println("Erreur de consultation");
            AirChance.menu();
        }

    }

}
